package web.suzy.oj.manager.admin.system;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import web.suzy.oj.dao.judge.JudgeEntityService;
import web.suzy.oj.dao.user.SessionEntityService;
import web.suzy.oj.dao.user.UserInfoEntityService;
import web.suzy.oj.pojo.entity.user.Session;
import web.suzy.oj.shiro.AccountProfile;

import java.util.List;
import java.util.Map;

/**
 * YangSuzy 软件工作室
 * 类名: DashboardManager
 * 描述: ---- 待定 -----
 * 功能: ---- 待定 -----
 *
 * @author YangSuzy
 * Date: 2022/11/30 10:50
 */
@Component
public class DashboardManager {
    @Autowired
    private SessionEntityService sessionEntityService;

    @Autowired
    private JudgeEntityService judgeEntityService;

    @Autowired
    private UserInfoEntityService userInfoEntityService;

    /**
     * 方法名: getRecentSession
     * 描述: 获取最近登录的会话
     *
     * @return web.suzy.oj.pojo.entity.user.Session
     * @date 2022/11/30 10:51
     * @auther YangSuzy
     **/
    public Session getRecentSession() {
        //需要获取一下该token对应用户的数据
        AccountProfile userRolesVo = (AccountProfile) SecurityUtils.getSubject().getPrincipal();
        //查询该管理员账号登录的历史会话，按登录时间降序排序
        QueryWrapper<Session> wrapper = new QueryWrapper<Session>()
                .eq("uid", userRolesVo.getUid())
                .orderByDesc("create_time");
        List<Session> sessionList = sessionEntityService.list(wrapper);
        if (sessionList.size() > 1) {
            //最近登录会话超过1个时返回上一个会话
            return sessionList.get(1);
        } else {
            return sessionList.get(0);
        }
    }

    /**
     * 方法名: getDashboardInfo
     * 描述: 获取工作台信息
     *
     * @return java.util.Map<java.lang.Object, java.lang.Object>
     * @date 2022/11/30 10:58
     * @auther YangSuzy
     **/
    public Map<Object, Object> getDashboardInfo() {
        int userNum = userInfoEntityService.count();
        int todayJudgeNum = judgeEntityService.getTodayJudgeNum();
        return MapUtil.builder()
                .put("userNum", userNum)
                .put("todayJudgeNum", todayJudgeNum).map();
    }
}
